//
//  AdverticePopView.m
//  carcareIOS
//
//  Created by wr on 16/5/10.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "AdverticePopView.h"
#import "ImageHelper.h"

@interface AdverticePopView()

@property (nonatomic , strong) UIImageView * adverticeImageV;

@property (nonatomic , copy) void (^btnClick)();

@property (nonatomic , strong) UIButton * adverticeBtn;

@property (nonatomic, assign) CGFloat maxWidth;
@property (nonatomic, assign) CGFloat maxHeight;

@end

@implementation AdverticePopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.maxWidth = frame.size.width;
        self.maxHeight = frame.size.height;
        self.backgroundColor = [UIColor clearColor];
        CGFloat viewX = 0;
        self.adverticeImageV = [Factory imageViewWithFrame:CGRectMake(viewX, viewX, self.frame.size.width - viewX * 2, self.frame.size.height - viewX * 2) image:nil onView:self];
        self.adverticeImageV.clipsToBounds = YES;
        self.adverticeImageV.layer.cornerRadius = 2;
        self.adverticeImageV.layer.masksToBounds = YES;
        self.adverticeImageV.contentMode = UIViewContentModeScaleAspectFit;
        
        __weak __typeof(self) wself = self;
        self.adverticeBtn = [[UIButton buttonEmptyWithFrame:self.adverticeImageV.frame click:^{
            if (wself.btnClick) wself.btnClick();
        }] setupOnView:self];
        
        [self.adverticeBtn setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}
- (void)raloadImageUrl:(NSString *)imageUrl withClick:(void(^)())imageBtnClick
{
    __weak __typeof(self) wself = self;
    
    [self.adverticeImageV setImageWithURLRequest:[ImageHelper requestURLWithURLStr:imageUrl] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
        wself.adverticeImageV.image = image;
        
        CGFloat height = wself.maxWidth /image.size.width * image.size.height;
        CGFloat width = wself.maxWidth;
        if (height > wself.maxHeight) {
            height = wself.maxHeight;
            width = wself.maxHeight / image.size.height * image.size.width;
        }
//        CGFloat width = image.size.width < wself.maxWidth ? image.size
        wself.adverticeImageV.size = CGSizeMake(width, height);
        wself.adverticeImageV.center = CGPointMake(wself.maxWidth/2, wself.maxHeight/2);
        wself.adverticeBtn.frame = wself.adverticeImageV.frame;
        if (imageBtnClick) {
            wself.btnClick = imageBtnClick;
        }
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        
    }];
    
    
//    [self.adverticeImageV setImageWithURL:[NSURL URLWithString:imageUrl]];
//    CGSize size = self.adverticeImageV.image.size;
    
//    self.adverticeImageV.size = size;
//    self.adverticeImageV.center =
//    self.adverticeBtn.frame = self.adverticeImageV.frame;
////    [self.adverticeBtn showDebugFrame];
////    [self.adverticeBtn setSize:self.adverticeImageV.image.size];
////    [self bringSubviewToFront:self.adverticeBtn];
//    if(imageBtnClick) self.btnClick = imageBtnClick;
}

- (void)makeAnimation
{
    
}
@end
